
#include	"SysConfig.h"
#include	"Define.h"
#include	"Display.h"
#include	"Function.h"

#ifdef  M16C_64G
#include	"Port.h"
#endif

#ifdef  PHONE_BOOK
#include	"PhoneBook.h"
#endif
#ifdef  MOVON
#include	"Bluetooth.h"
#include	"Movon.h"

unsigned char ucMV_UART_RX_Buf[MV_RX_SIZE];
unsigned short	usMV_UART_RX_wPtr;
unsigned short	usMV_UART_RX_rPtr;
unsigned char ucMV_RX_Mode;
#define	MV_RX_NONE				0
#define	MV_RX_LENGTH			1
#define	MV_RX_DATA				2
#define	MV_RX_PB_HEADER			3
#define	MV_RX_PB_LENGTH			4
#define	MV_RX_PB_EVENT_ID		5
#define	MV_RX_PB_DATA_RECEIVE	6
unsigned char ucMV_RX_Length;
unsigned short usMV_RX_PB_Length;
unsigned char ucMV_RX_PB_MoreData;
unsigned char ucMV_HF_AudioStatus_Timer50msec;
unsigned char ucMV_CheckCallAudio;
unsigned char ucBT_FuncOff;

unsigned char ucMV_PB_SkipBuf[2];
unsigned char ucMV_PB_SkipBufCnt;
unsigned char ucMV_PB_DataSkip;

unsigned char ucMV_BTOnOff;
unsigned char ucMV_InitOk;
unsigned char ucMV_BootOn;
unsigned char ucMV_PairingStatus;
MV_HF_STATUS ucMV_HFS_Status;
MV_AV_STATUS ucMV_A2DP_Status;
MV_AVRCP_STATUS ucMV_AVRCP_Status;
MV_PB_STATUS ucMV_PBAP_Status;
MV_MIC_STATUS ucMV_MIC_Mute;
MV_CALL_STATUS ucMV_CallStatus;
MV_HF_AUDIO ucMV_HF_AudioConnect;	// 0 : Phone, 1 : Audio

MV_HF_PROFILE ucMV_HF_Profile;
extern BT_PROC ucBT_Process;
extern unsigned char ucBT_ProcessSeq;
extern unsigned short usBT_ProcessSeq_Timer50msec;
extern BT_DEVICE	sBT_BDList[BT_DEVICE_NUM];
extern unsigned char ucBT_DeviceCnt;
extern unsigned char ucBT_LocalName[BT_LOCALNAME_SIZE];
extern unsigned char ucBT_LocalAddress[6];
extern unsigned char ucBT_PairedDeviceIndex;
extern unsigned char ucBT_CallerID[BT_CALLERID_LEN];
extern unsigned char ucDISP_BT_CallerID[BT_CALLERID_LEN];
extern unsigned char ucBT_DeleteRequestIndex;
extern unsigned short	usBT_AutoConnect_Timer100msec;
extern BT_DEVICE	sBT_ConnectedDevice;
extern unsigned char ucPBAP_SearchResult;
extern unsigned char ucDISP_PBAPSearchResult;
extern unsigned char ucBT_RootCall;
extern unsigned char ucPWR_A2DP_Resume;
extern unsigned char ucBT_UserReject;
extern unsigned char ucMV_CheckCallAudio;
extern unsigned char ucFUNC_Power;

extern void BT_WriteBD(unsigned char ucMode,unsigned char *ucBuf, unsigned char ucLen);

extern void BT_AutoConnectionStart(void);
extern void BT_AutoConnectionStop(void);
extern unsigned char BT_GetBDNumberByAddress(unsigned char *ucAdrs);
extern void BT_PBAP_SetDownload(unsigned char ucDes);

extern void DISP_SetTempDisplayMode_6Sec(UI_DISP ucDes);
extern void MENU_Clear_RecentCallID(void);

#ifdef  PHONE_BOOK
extern void PBAP_InitBlockData(void);
extern unsigned char ucPBAP_CurrentContents;
extern void PBAP_SearchStart(void);
#endif
#ifdef  SERIAL_FLASH
extern void SF_WriteCloseRequest(void);
#endif

extern void BT_AVRCP_Command(BT_AVRCP ucCmd);
extern void MV_TX_BootMode(unsigned char ucMode);

void MV_RX_WriteBuf(unsigned char ucData)	// Call Uart int
{
	ucMV_UART_RX_Buf[usMV_UART_RX_wPtr] = ucData;
	++usMV_UART_RX_wPtr;
	usMV_UART_RX_wPtr = usMV_UART_RX_wPtr % MV_RX_SIZE;
}

unsigned char MV_RX_GetData(void)	// used parsing in received
{
	unsigned char ucData;

	ucData = ucMV_UART_RX_Buf[usMV_UART_RX_rPtr];
	++usMV_UART_RX_rPtr;
	usMV_UART_RX_rPtr = usMV_UART_RX_rPtr % MV_RX_SIZE;

	return(ucData);
}

void MV_Set_AudioConnectAG(void)
{
	ucMV_HF_AudioConnect = HF_AUDIO_AG;
}

unsigned short MV_RX_GetDataCnt(void)
{
	if  (usMV_UART_RX_wPtr == usMV_UART_RX_rPtr)
	{
		return(0);
	}

	if  (usMV_UART_RX_wPtr > usMV_UART_RX_rPtr)
	{
		return(usMV_UART_RX_wPtr - usMV_UART_RX_rPtr);
	}

	return(usMV_UART_RX_wPtr + MV_RX_SIZE - usMV_UART_RX_rPtr);
}

void MV_RX_BootOn(unsigned char ucNum, unsigned char *ucBuf)
{	
	LOG_BT("MV_RX_BootOn");
	ucMV_BootOn = 1;
}

void MV_RX_OnOff(unsigned char ucNum, unsigned char *ucBuf)
{
	ucMV_BTOnOff = *ucBuf;	// 0x00 : OFF
							// 0x01 : On
	LOG_BT("MV_RX_OnOff:%d",ucMV_BTOnOff);
}

void MV_RX_Start(unsigned char ucNum, unsigned char *ucBuf)
{
	ucMV_InitOk = 1;
	LOG_BT("MV_RX_Start:%d",ucMV_InitOk);
}

void MV_RX_PairingStatus(unsigned char ucNum, unsigned char *ucBuf)
{
	ucMV_PairingStatus = *ucBuf;
	LOG_BT("MV_RX_PairingStatus:%d",*ucBuf);
}

void MV_RX_PairStatus(unsigned char ucNum, unsigned char *ucBuf)
{
	unsigned char ucParam;

	ucParam = *ucBuf;

	LOG_BT("MV_RX_PairStatus:%d",ucParam);

	if  ((ucBT_Process == BT_PRS_PAIRING) && (ucBT_ProcessSeq == BT_PAIRING_WAIT))
	{
		if  (ucParam == 0x00)	// Success
		{
			ucBT_ProcessSeq = BT_PAIRING_SUCCESS;
		}
		else	// Fail
		{
			ucBT_ProcessSeq = BT_PAIRING_FAIL;
		}
	}
	LOG_BT("%d",ucBT_ProcessSeq);
}

void MV_RX_ReadPairedDevice(unsigned char ucNum, unsigned char *ucBuf)
{
	unsigned char i;
	unsigned char j;
	unsigned char ucNewCnt;
	BT_DEVICE	sBDList[BT_DEVICE_NUM];

	LOG_BT("MV_RX_ReadPairedDevice,%d",ucBT_ProcessSeq);
	
	ucNewCnt = *ucBuf;

	if  (ucNewCnt > BT_DEVICE_NUM)
	{
		ucNewCnt = BT_DEVICE_NUM;
	}
	++ucBuf;
#if  1
	for(i = 0; i < BT_DEVICE_NUM; ++i)
	{
		memset(&sBDList[i].ucDeviceAdr[0], 0x00, sizeof(BT_DEVICE));
	}
	for(i = 0; i < ucNewCnt; ++i)
	{
		memcpy(&sBDList[i].ucDeviceAdr[0], ucBuf + i * 6, 6);
		sBDList[i].ucOrder = i;
	}

	for(i = 0; i < ucNewCnt; ++i)
	{
		ucNum = BT_GetBDNumberByAddress(&sBDList[i].ucDeviceAdr[0]);

		if  (ucNum < ucBT_DeviceCnt)
		{
			memcpy(&sBDList[i].ucName[0], &sBT_BDList[ucNum].ucName[0], BT_DEVICE_NAME_LENGTH);
		}
	}

	for(i = 0; i < BT_DEVICE_NUM; ++i)
	{
		memset(&sBT_BDList[i].ucDeviceAdr[0], 0x00, sizeof(BT_DEVICE));	// Clear All
	}
	for(i = 0; i < ucNewCnt; ++i)
	{		
		memcpy(&sBT_BDList[i].ucDeviceAdr[0],&sBDList[i].ucDeviceAdr[0], sizeof(BT_DEVICE));	// Copy All
	}
	ucBT_DeviceCnt = ucNewCnt;
#else
	for(i = 0; i < ucBT_DeviceCnt; ++i)
	{
		memcpy(&sBT_BDList[i].ucDeviceAdr[0], ucBuf + i * 6, 6);
		sBT_BDList[i].ucOrder = i;
	//	MV_TX_ReadRemoteNameToBDAddr(sBT_BDList[i]);
	}
#endif
	ucBT_PairedDeviceIndex = 0;	// After find new device
	memcpy(&sBT_ConnectedDevice.ucDeviceAdr[0], &sBT_BDList[ucBT_PairedDeviceIndex].ucDeviceAdr[0], 6);
#if  0

///////////////// Back Up /////////////////////////////////////////
	for(i = 0; i < BT_DEVICE_NUM; ++i)
	{
		memset(&sBDList[i].ucDeviceAdr[0], 0x00, BT_DEVICE_NAME_LENGTH + 8);
		memcpy(&sBT_BDList[i].ucDeviceAdr[0], &sBDList[i].ucDeviceAdr[0], 6);
		memset(&sBT_BDList[i].ucDeviceAdr[0], 0x00, BT_DEVICE_NAME_LENGTH + 8);
	}
	ucOldCnt = ucBT_DeviceCnt;
///////////////////////////////////////////////////////////////////
	ucBT_DeviceCnt = *ucBuf;

	
	for(i = 0; i < ucBT_DeviceCnt; ++i)
	{
		memcpy(&sBT_BDList[i].ucDeviceAdr[0], ucBuf, 6);
	}

	if  (ucBT_DeviceCnt == ucOldCnt)
	{
	}
	else  if  (ucBT_DeviceCnt > ucOldCnt)	// New Paired
	{
		for(i = 0;i < ucBT_DeviceCnt; ++i)
		{
			for(j = 0; j < ucOldCnt; ++j)
			{
				if  (memcmp(&sBT_BDList[i].ucDeviceAdr[0], &sBDList[j].ucDeviceAdr[0], 6) == 0)
				{
					memcpy(&sBT_BDList[i].ucName[0], &sBDList[j].ucName[0], BT_DEVICE_NAME_LENGTH);
					sBT_BDList[i].ucOrder = sBDList[j].ucOrder + 1;
					sBT_BDList[i].ucService = sBDList[j].ucService;
				}
			}
		}
		for(i = 0; i < ucBT_DeviceCnt; ++i)
		{
			if  (sBT_BDList[i].ucOrder == 0)
			{
				ucBT_PairedDeviceIndex = i;
			}
		}	
	}
	else	// Deleted
	{
		for(i = 0;i < ucBT_DeviceCnt; ++i)
		{
			for(j = 0; j < ucOldCnt; ++j)
			{
				if  (memcmp(&sBT_BDList[i].ucDeviceAdr[0], &sBDList[j].ucDeviceAdr[0], 6) == 0)
				{
					memcpy(&sBT_BDList[i].ucName[0], &sBDList[j].ucName[0], BT_DEVICE_NAME_LENGTH);
					if  (sBDList[j].ucOrder > sBDList[ucBT_DeleteRequestIndex].ucOrder)
					{
						sBT_BDList[i].ucOrder = sBDList[j].ucOrder - 1;
					}
					sBT_BDList[i].ucService = sBDList[j].ucService;
					memcpy(&sBT_BDList[i].ucName[0], &sBDList[j].ucName[0], BT_DEVICE_NAME_LENGTH);
				}
			}
		}
	}

	
	LOG_BT("%d",ucBT_ProcessSeq);
	#endif
}

void MV_RX_ClearALLPairedDeviceStatus(unsigned char ucNum, unsigned char *ucBuf)
{
	unsigned char ucParam;
	unsigned char i;

	ucParam = *ucBuf;

	if  (ucParam == 0x00)	// Success
	{
		for(i = 0; i < BT_DEVICE_NUM; ++i)
		{
			memset(&sBT_BDList[i].ucDeviceAdr[0], 0x00, sizeof(BT_DEVICE));
		}
		LOG_BT("MV_RX_ClearALLPairedDeviceStatus:Success");
		ucBT_DeviceCnt = 0;
	}
	else	// Fail
	{		
		LOG_BT("MV_RX_ClearALLPairedDeviceStatus:Fail");
		LOG_BT("Par:%d",ucParam);
	}
}

void MV_RX_ClearPairedDeviceStatus(unsigned char ucNum, unsigned char *ucBuf)
{
	unsigned char ucParam;

	ucParam = *ucBuf;

	if  ((ucBT_Process == BT_PRS_DELETE) && (ucBT_ProcessSeq == BT_DELETE_WAIT_DELETE))
	{
		if  (ucParam == 0x00)	// Success
		{
		}
		else	// Fail
		{
		}
		ucBT_ProcessSeq = BT_DELETE_REQ_LIST;
	}
	LOG_BT("MV_RX_ClearPairedDeviceStatus:%d",ucParam);
}

void MV_HF_StatusUpdate(unsigned char ucStatus)
{
	MV_HF_STATUS ucHF_StatusLast;
	
	LOG_BT("MV_HF_StatusUpdate:%X", ucStatus);
	if  (ucMV_HFS_Status != ucStatus)
	{
		ucHF_StatusLast = ucMV_HFS_Status;
		ucMV_HFS_Status = ucStatus;
		switch  (ucStatus)
		{
			case MV_HFS_INITIALISING:
				if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_HF))
				{
					FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
				}
				break;
			case MV_HFS_DISCORVERABLE:
				if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_HF))
				{
					FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
				}
				break;
			case MV_HFS_CONNECTABLE:
				if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_HF))
				{
					FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
				}
				break;
			case MV_HFS_CONNECTED:
				ucBT_RootCall = 0;
				if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_HF))
				{
					ucMV_MIC_Mute = 0;
					if  (ucBT_FuncOff == 1)
					{
						FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(),FUNC_CHANGE_SEQ0 );
					}
					else
					{
						FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_POWER_OFF_SEQ0);
					}
					
					if  (ucHF_StatusLast == MV_HFS_ACTIVE_CALL)
					{
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
					}
					else  if  (ucHF_StatusLast == MV_HFS_INCOMING)
					{
						if  (ucBT_UserReject == 1)
						{
							DISP_SetTempDisplayMode(DISP_BT_CALL_REJECT);
						}
						else
						{
							ucDISP_PBAPSearchResult = 0;
							if  (ucPBAP_SearchResult == 1)
							{
								ucDISP_PBAPSearchResult = 1;
							}
							DISP_SetTempDisplayMode_6Sec(DISP_BT_CALL_MISS);
							memcpy(&ucDISP_BT_CallerID[0], &ucBT_CallerID[0], BT_CALLERID_LEN);
						}
					}
					else  if  (ucHF_StatusLast == MV_HFS_OUTGOING)
					{
						DISP_SetTempDisplayMode(DISP_BT_CALL_END);
					}
					memset(&ucBT_CallerID[0], 0x00, BT_CALLERID_LEN);
				}
				BT_PBAP_SetDownload(1);
				ucBT_UserReject = 0;
				break;
			case MV_HFS_OUTGOING:
				BT_PBAP_SetDownload(2);
				if  (FUNC_GetCurrentFunction() != FUNC_HF)
				{
					if  (ucFUNC_Power == 1)
					{
						ucBT_FuncOff = 1;	
					}
					else
					{
						ucBT_FuncOff = 0;							
					}
					FUNC_FunctionChangeStart(FUNC_HF, FUNC_CHANGE_SEQ0);
					MV_TX_HF_GetCurrentCalls();
				}
				if  (ucHF_StatusLast != MV_HFS_OUTGOING)
				{
					ucMV_CheckCallAudio = 1;
				}
				MENU_Clear_RecentCallID();
				break;
			case MV_HFS_INCOMING:
				BT_PBAP_SetDownload(2);
				if  (FUNC_GetCurrentFunction() != FUNC_HF)
				{
					if  (ucFUNC_Power == 1)
					{
						ucBT_FuncOff = 1;	
					}
					else
					{
						ucBT_FuncOff = 0;							
					}
					FUNC_FunctionChangeStart(FUNC_HF, FUNC_CHANGE_SEQ0);
					DISP_SetTempDisplayMode(DISP_BT_CALL_IN);
				}
				break;
			case MV_HFS_ACTIVE_CALL:
				BT_PBAP_SetDownload(2);
				if  (FUNC_GetCurrentFunction() != FUNC_HF)
				{
					if  (FUNC_PowerStatus() == 1)
					{
						ucBT_FuncOff = 1;	
					}
					else
					{
						ucBT_FuncOff = 0;							
					}
					FUNC_FunctionChangeStart(FUNC_HF, FUNC_CHANGE_SEQ0);
					MV_TX_HF_GetCurrentCalls();
					if  (ucHF_StatusLast == MV_HFS_INCOMING)
					{
						DISP_SetTempDisplayMode(DISP_BT_ACCPET_CALL);
					}
				}
				if  ((ucHF_StatusLast != MV_HFS_OUTGOING) && (ucHF_StatusLast != MV_HFS_ACTIVE_CALL))
				{
					ucMV_CheckCallAudio = 1;
				}
				break;
			case MV_HFS_3WC_WAITING:
				break;
			case MV_HFS_3WC_HOLD:
				break;
			case MV_HFS_3WC_MULTI:
				break;
		}
	}	
}

void MV_PB_StatusUpdate(unsigned char ucStatus)
{
	MV_PB_STATUS ucPBAP_StatusLast;
	LOG_BT("MV_PB_StatusUpdate:%d",ucStatus);
	if  (ucMV_PBAP_Status != ucStatus)
	{
		ucPBAP_StatusLast = ucMV_PBAP_Status;
		ucMV_PBAP_Status = ucStatus;
		switch  (ucStatus)
		{
			case MV_PBAP_READY:
				if  (ucPBAP_StatusLast == MV_PBAP_CONNECTING)
				{
					if  ((ucBT_Process == BT_PRS_PABP_DOWNLOAD) && (ucBT_ProcessSeq == BT_PBAP_WAIT_CON))
					{
						ucBT_Process = BT_PRS_NONE;
						LOG_BT("Cancel");
					}
				}
				break;
			case MV_PBAP_CONNECTING:
				break;
			case MV_PBAP_CONNECTED:
				break;
			case MV_PBAP_DOWNLOADING:
				break;
			case MV_PBAP_NO_SERVICE:
				break;
		}
	}
}

void MV_AV_StatusUpdate(unsigned char ucStatus)
{
	MV_AV_STATUS ucA2DP_StatusLast;
	
	LOG_BT("MV_AV_StatusUpdate:%d",ucStatus);
	if  (ucMV_A2DP_Status != ucStatus)
	{
		ucA2DP_StatusLast = ucMV_A2DP_Status;
		ucMV_A2DP_Status = ucStatus;
		switch  (ucStatus)
		{
			case MV_A2DP_CONNECTABLE:
				if  (ucA2DP_StatusLast > MV_A2DP_CONNECTABLE)
				{
					if  (ucPWR_A2DP_Resume == 0)
					{
						if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_A2DP))
						{
							FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
						}
					}
				}
				break;
			case MV_A2DP_SIGNALING_CHANNEL_CONNECTED:
				if  (ucA2DP_StatusLast == MV_A2DP_CONNECTABLE)
				{
					ucPWR_A2DP_Resume = 0;
				}
				break;
			case MV_A2DP_CONNECTED:
				if  (ucA2DP_StatusLast == MV_A2DP_CONNECTABLE)
				{
					ucPWR_A2DP_Resume = 0;
				}
				break;
			case MV_A2DP_STREAMING:
				if  (ucA2DP_StatusLast == MV_A2DP_CONNECTABLE)
				{
					ucPWR_A2DP_Resume = 0;
				}
				break;
			case MV_A2DP_PAUSED:
				if  (ucA2DP_StatusLast == MV_A2DP_CONNECTABLE)
				{
					ucPWR_A2DP_Resume = 0;
				}
				break;
		}
	}
}

void MV_AVRCP_StatusUpdate(unsigned char ucStatus)
{
	MV_AVRCP_STATUS ucAVRCP_StatusLast;
	LOG_BT("MV_AVRCP_StatusUpdate");
	if  (ucMV_AVRCP_Status != ucStatus)
	{
		ucAVRCP_StatusLast = ucMV_AVRCP_Status;
		ucMV_AVRCP_Status = ucStatus;
		switch  (ucStatus)
		{
			case MV_AVRCP_INITIALISING:
				LOG_BT("INITIALISING");
				break;
			case MV_AVRCP_READY:
				LOG_BT("READY");
				break;
			case MV_AVRCP_CONNECTING:
				LOG_BT("CONNECTING");
				break;
			case MV_AVRCP_CONNECTED:
				if  (ucAVRCP_StatusLast < MV_AVRCP_CONNECTED)
				{
					LOG_BT("CONNECTED");
					LOG_BT("P:%d,F:%d",FUNC_PowerStatus(),FUNC_GetCurrentFunction());
					if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_A2DP))
					{
						BT_AVRCP_Command(BT_AVRCP_PLAY_PAUSE);
					}
				}
				break;
		}
	}
}

void MV_RX_GetDeviceStatus(unsigned char ucNum, unsigned char *ucBuf)
{	
	unsigned char ucPairingStatus;

	ucPairingStatus = *ucBuf;
	MV_HF_StatusUpdate(*(ucBuf + 1));
	MV_AV_StatusUpdate(*(ucBuf + 2));
	MV_AVRCP_StatusUpdate(*(ucBuf + 3));
	MV_PB_StatusUpdate(*(ucBuf + 4));
	LOG_BT("MV_RX_GetDeviceStatus");
	LOG_BT("HF:%d",*(ucBuf + 1));
	LOG_BT("AV:%d",*(ucBuf + 2));
	LOG_BT("AVRCP:%d",*(ucBuf + 3));
	LOG_BT("PB:%d",*(ucBuf + 4));
}

void MV_RX_ReadLocalName(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_ReadLocalName");
	if  (ucLen > BT_LOCALNAME_SIZE)
	{
		ucLen = BT_LOCALNAME_SIZE;
	}
	memcpy(&ucBT_LocalName[0], ucBuf, ucLen);
}

void MV_RX_ReadLocalBDAddress(unsigned char ucLen, unsigned char *ucBuf)
{
	memcpy(&ucBT_LocalAddress[0], ucBuf, 6);
	LOG_BT("MV_RX_ReadLocalBDAddress");
}

void MV_RX_ReadLocalClassOfDevice(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_ReadLocalClassOfDevice");
}

void MV_RX_ReadRemoteName(unsigned char ucLen, unsigned char *ucBuf)
{
	BT_WriteBD(0x01, ucBuf, *(ucBuf + 6));
	LOG_BT("MV_RX_ReadRemoteName");	
}

void MV_RX_GetRSSI(unsigned char ucLen, unsigned char *ucBuf)
{
	signed char ucRSSI;

	ucRSSI = *ucBuf;
	LOG_BT("MV_RX_GetRSSI");
}

void MV_RX_GetPinCode(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucPinCode[4];

	memcpy(&ucPinCode[0], ucBuf, 4);
	LOG_BT("MV_RX_GetPinCode");
}

void MV_RX_HF_StateInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucHF_Status;

	ucHF_Status = *ucBuf;
	
	LOG_BT("MV_RX_HF_StateInd");
	MV_HF_StatusUpdate(ucHF_Status);
}

void MV_RX_HF_ConnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucStatus;

	ucStatus = *ucBuf;

	LOG_BT("MV_RX_HF_ConnectStatusInd:%d",ucStatus);
	if  (ucStatus == 0x00)	// Success
	{
		BT_AutoConnectionStop();
	}
	else	// Fail
	{
		if  ((ucBT_Process == BT_PRS_CONNECT) && (ucBT_ProcessSeq == BT_CON_SEQ_WAIT_C))
		{
			ucBT_ProcessSeq = BT_CON_SEQ_FAIL;
		}
	}
}

void MV_RX_HF_ConnectProfileInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucHF_Profile;

	ucMV_HF_Profile = *ucBuf;
	LOG_BT("MV_RX_HF_ConnectProfileInd");
}

void MV_RX_HF_DisconnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{	
	LOG_BT("MV_RX_HF_DisconnectStatusInd:%d",*ucBuf);
	DISP_SetTempDisplayMode(DISP_BT_NOCONNECT); 
	if  (*ucBuf == 0x00)	// Success
	{
		//DISP_SetTempDisplayMode(DISP_BT_NOCONNECT); 
	}
	else if  (*ucBuf == 0x01)	// Link-Loss
	{
		BT_AutoConnectionStart();
		usBT_AutoConnect_Timer100msec = 60000/100;
	}
	else	// Fail
	{
		
	}
}

void MV_RX_HF_AudioConnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_AudioConnectStatusInd:%d", *ucBuf);
	LOG_BT("%d", ucMV_CheckCallAudio);
	LOG_BT("%d", ucBT_RootCall);
	if  (*ucBuf == 0x00)	// Success
	{	
		if  ((ucMV_CheckCallAudio == 1) && (ucBT_RootCall == 0))
		{
			MV_TX_HF_AudioTransfer();
		}
		else
		{
			MV_Set_AudioConnectAG();
		}	
	}
	else	// Fail
	{
	}
	ucMV_CheckCallAudio = 0;
}

void MV_RX_HF_AudioDisconnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_AudioDisconnectStatusInd:%d",*ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
		ucMV_HF_AudioConnect = HF_AUDIO_PHONE;
		ucMV_HF_AudioStatus_Timer50msec = 200/50;
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_SpeakerVolumeChangeInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucVol;

	ucVol = *ucBuf;
	LOG_BT("MV_RX_HF_SpeakerVolumeChangeInd");
}

void MV_RX_HF_ReadSpeakerVolumeRes(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucVol;

	ucVol = *ucBuf;
	LOG_BT("MV_RX_HF_ReadSpeakerVolumeRes");
}

void MV_RX_HF_MicMuteStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	ucMV_MIC_Mute = *ucBuf;	// Disabled(0x00), Enabled(0x01)
	LOG_BT("MV_RX_HF_MicMuteStatusInd:%d", ucMV_MIC_Mute);
	
	if  ((ucBT_Process == BT_PRS_MIC_MUTE) && (ucBT_ProcessSeq == BT_COM_SEQ_WAIT_CFM))
	{
		usBT_ProcessSeq_Timer50msec = 0;
	}
}

void MV_RX_HF_AnswerCallStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_AnswerCallStatusCfm:%d", *ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
		MV_Set_AudioConnectAG();
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_ANSWER_CALL)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_HangUpCallStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_HangUpCallStatusCfm:%d",*ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_HANDUP_CALL)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_RejectCallStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_RejectCallStatusCfm:%d", *ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
		DISP_SetTempDisplayMode(DISP_BT_CALL_REJECT);
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_REJECT_CALL)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_LastNumberRedialStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_LastNumberRedialStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
		DISP_SetTempDisplayMode(DISP_BT_REDIAL);
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_REDIAL)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_NumberDialStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_NumberDialStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_DIALNUMBER)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_DTMFToneStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_DTMFToneStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_VoiceDialActivationStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_VoiceDialActivationStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallsReleaseHeldOrRejectWaitingStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallsReleaseHeldOrRejectWaitingStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallsReleaseActiveAcceptOtherStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallsReleaseActiveAcceptOtherStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallsHoldActiveAcceptOtherStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallsHoldActiveAcceptOtherStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallAddHeldCallStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallAddHeldCallStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallsExplicitCallTransferStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallsExplicitCallTransferStatusCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_GetNetworkOperatorInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucMode;
	unsigned char ucNetwork[9];

	ucMode = *ucBuf;
	ucLen = *(ucBuf + 1);
	LOG_BT("MV_RX_HF_GetNetworkOperatorInd");
	if  (ucLen > 9)
	{
		ucLen = 9;
	}
	memcpy(&ucNetwork[0], ucBuf, 9);
}

void MV_RX_HF_GetSubscriberNumberInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_GetSubscriberNumberInd");
}

void MV_RX_HF_GetCurrentCallInd(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucCallNum;
	unsigned char ucDirection;
	unsigned char ucMode;
	unsigned char ucMultiparty;
	unsigned char ucNumberType;
	unsigned char ucNumLen;

	ucCallNum = *ucBuf;	++ucBuf;
	ucDirection = *ucBuf;++ucBuf;
	ucMV_CallStatus = *ucBuf;++ucBuf;
	ucMode = *ucBuf;++ucBuf;
	ucMultiparty = *ucBuf;++ucBuf;
	ucNumberType= *ucBuf;++ucBuf;
	ucNumLen = *ucBuf;++ucBuf;
	LOG_BT("MV_RX_HF_GetCurrentCallInd");
	if  (ucNumLen > BT_CALLERID_LEN)
	{
		ucNumLen = BT_CALLERID_LEN;
	}
	memset(&ucBT_CallerID[0], 0x00, BT_CALLERID_LEN);
	memcpy(&ucBT_CallerID[0],ucBuf,ucNumLen);
	PBAP_SearchStart();
}

void MV_RX_HS_SendButtonPressCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HS_SendButtonPressCfm");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_RingInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_RingInd");
}

void MV_RX_HF_InbandRingInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_InbandRingInd");
	if  (*ucBuf == 0x00)	// Disabled
	{
	}
	else	// Enabled
	{
	}
}

void MV_RX_HF_CallInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallInd:%d", *ucBuf);
	if  (*ucBuf == 0x00)	// Call Inactive
	{
	//	if  (FUNC_GetCurrentFunction() == FUNC_HF)
	//	{
	//		FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
	//		DISP_SetTempDisplayMode(DISP_BT_CALL_IN);
	//	}
	}
	else	// Call Active
	{
	//	if  (FUNC_GetCurrentFunction() != FUNC_HF)
	//	{
	//		FUNC_FunctionChangeStart(FUNC_HF, FUNC_CHANGE_SEQ0);
	//		DISP_SetTempDisplayMode(DISP_BT_CALL_IN);
	//	}
	}
}

void MV_RX_HF_CallerID(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucNumLen;
	unsigned char ucNational;

	ucNational = *ucBuf; ++ucBuf;
	ucNumLen = *ucBuf; ++ucBuf;
	
	if  (ucNumLen > BT_CALLERID_LEN)
	{
		ucNumLen = BT_CALLERID_LEN;
	}
	memset(&ucBT_CallerID[0], 0x00, BT_CALLERID_LEN);
	memcpy(&ucBT_CallerID[0],ucBuf,ucNumLen);
	LOG_BT("MV_RX_HF_CallerID");
	PBAP_SearchStart();
//	LOG_BT("%S",&ucBT_CallerID[0]);
}

void MV_RX_HF_AudioTransferToAGInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_AudioTransferToAGInd");
	if  (*ucBuf == 0x00)	// Success
	{
	//	MV_Set_AudioConnectAG();
	}
	else	// Fail
	{
	}
	if  (ucBT_Process == BT_PRS_CALL_TRANS)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_AudioTransferToHF(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_AudioTransferToHF");
	if  (*ucBuf == 0x00)	// Success
	{
	//	ucMV_HF_AudioConnect = HF_AUDIO_PHONE; // private
	}
	else	// Enabled
	{
	}
	if  (ucBT_Process == BT_PRS_CALL_TRANS)
	{
		ucBT_Process = BT_PRS_NONE;
	}
}

void MV_RX_HF_SpeedCall(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_SpeedCall");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_CallWaitingInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_HF_CallWaitingInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_PB_NumOfPhonebook(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned short usPBNum;

	usPBNum = *ucBuf;
	++ucBuf;
	usPBNum = usPBNum | *ucBuf; 
	LOG_BT("MV_RX_PB_NumOfPhonebook");
}

void MV_RX_PB_PhoneBookDownloadCompleteInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_PB_PhoneBookDownloadCompleteInd");
//	if  ((ucBT_Process == BT_PRS_PABP_DOWNLOAD) && (ucBT_ProcessSeq == BT_PBAP_DOWNLOADING))
//	{
//		ucBT_Process = BT_PRS_NONE;
//	}
//	if  ((ucBT_Process == BT_PRS_CALLHISTORY_DOWNLOAD) && (ucBT_ProcessSeq == BT_PBAP_DOWNLOADING))
//	{
//		ucBT_Process = BT_PRS_NONE;
//	}

	if  (ucPBAP_CurrentContents == PBAP_CONTENTS_PHONEBOOK)
	{
		DISP_SetTempDisplayMode_6Sec(DISP_BT_PHONEBOOK_DOWNLOAD_END);
	}
	SF_WriteCloseRequest();
}

void MV_RX_PB_ConnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_PB_ConnectStatusInd:%d",*ucBuf);
	if  (*ucBuf == 0x00)
	{
	}
	else	// Fail
	{
		if  ((ucBT_Process == BT_PRS_PABP_DOWNLOAD) && (ucBT_ProcessSeq == BT_PBAP_WAIT_CON))
		{
			ucBT_Process = BT_PRS_NONE;
			LOG_BT("Cancel");
		}
		if  ((ucBT_Process == BT_PRS_CALLHISTORY_DOWNLOAD) && (ucBT_ProcessSeq == BT_PBAP_WAIT_CON))
		{
			ucBT_Process = BT_PRS_NONE;
			LOG_BT("Cancel");
		}
	}
}

void MV_RX_PB_StatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	MV_PB_StatusUpdate(*ucBuf);
	LOG_BT("MV_RX_PB_StatusInd");
}

void MV_RX_AV_StatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_StatusInd:%d",*ucBuf);
	MV_AV_StatusUpdate(*ucBuf);
}

void MV_RX_AV_ConnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_ConnectStatusInd:%d",*ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
		BT_AutoConnectionStop();
		if  (*(ucBuf + 1) == 0x00)	// Connect signalling channel
		{
		}
		else  if  (*(ucBuf + 1) == 0x01)	// Connect Media
		{
		}
	}
	else	// Fail
	{
		ucPWR_A2DP_Resume = 0;
	}	
}

void MV_RX_AV_DisconnectStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_DisconnectStatusInd:%d", *ucBuf);
	switch  (*ucBuf)
	{
		case 0x00:	// Success
	//		if  (ucPWR_A2DP_Resume == 0)
	//		{
	//			if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_A2DP))
	//			{
	//				FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
	//			}
	//		}
			break;
		case 0x01:	// Link Loss
			if  ((FUNC_PowerStatus()) && (FUNC_GetCurrentFunction() == FUNC_A2DP))
			{
				FUNC_FunctionChangeStart(FUNC_GetPreviousFunction(), FUNC_CHANGE_SEQ0);
			}			
			DISP_SetTempDisplayMode(DISP_BT_NOCONNECT);
			BT_AutoConnectionStart();
			usBT_AutoConnect_Timer100msec = 60000/100;
			break;
		case 0x02:	// Fail
			break;
	}
}

void MV_RX_AV_SpeakerVolumeChangeInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_SpeakerVolumeChangeInd");
}

void MV_RX_AV_ReadSpeakerVolumeRes(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_ReadSpeakerVolumeRes");

}

void MV_RX_AV_SendRemoteControlCommandStatusCfm(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_AV_SendRemoteControlCommandStatusCfm:%d",*ucBuf);
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}

	if  (ucBT_Process == BT_PRS_AVRCP_CMD)
	{
		ucBT_ProcessSeq = BT_PRS_AVRCP_CMD_REL_MUTE;
		usBT_ProcessSeq_Timer50msec = 200/50;
	}
}

void MV_RX_AVRCP_StatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	MV_AVRCP_StatusUpdate(*ucBuf);
	LOG_BT("MV_RX_AVRCP_StatusInd");
}

unsigned char ucMV_FirmwareVersion[4];

void MV_RX_SYS_FW_VersionRes(unsigned char ucLen, unsigned char *ucBuf)
{
	unsigned char ucVersion[4];

	memcpy(&ucMV_FirmwareVersion[0], ucBuf, 4);
	LOG_BT("MV_RX_SYS_FW_VersionRes");
	LOG_BT("%X",ucMV_FirmwareVersion[0]);
	LOG_BT("%X",ucMV_FirmwareVersion[1]);
	LOG_BT("%X",ucMV_FirmwareVersion[2]);
	LOG_BT("%X",ucMV_FirmwareVersion[3]);
}

void MV_RX_SYS_EnterDUTModeStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_SYS_EnterDUTModeStatusInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

extern void MV_TX_HF_ReadMICVolume_Req(void);

void MV_RX_SYS_SPK_MIC_LoopbackStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_SYS_SPK_MIC_LoopbackStatusInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_SYS_Enter_DFU_ModeStatusInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_SYS_Enter_DFU_ModeStatusInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_HF_MIC_VolumeChangeInd(void)
{
	LOG_BT("MV_RX_HF_MIC_VolumeChangeInd");
}

extern unsigned char ucBT_MIC_Volume;

void MV_RX_HF_Read_MIC_VolumeRes(unsigned char *ucVol)
{
	ucBT_MIC_Volume = *ucVol;
	LOG_BT("MV_RX_HF_Read_MIC_VolumeRes:%d",ucBT_MIC_Volume);
}

void MV_RX_UART_REQ_SuccessInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_UART_REQ_SuccessInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_UART_REQ_FailInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_UART_REQ_FailInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

void MV_RX_ErrorInd(unsigned char ucLen, unsigned char *ucBuf)
{
	LOG_BT("MV_RX_ErrorInd");
	if  (*ucBuf == 0x00)	// Success
	{
	}
	else	// Fail
	{
	}
}

unsigned char ucMV_DataBuf[128];

void MV_RX_EventMain(void)
{
	unsigned char ucLen = ucMV_RX_Length;
	unsigned char ucEventID;
	unsigned char i;

	ucEventID = MV_RX_GetData();

	for(i = 0; i < (ucLen - 1); ++i)
	{
		ucMV_DataBuf[i] = MV_RX_GetData();
	}

	if  (ucLen == 0)
	{
		return;
	}
	ucLen = ucLen - 1;

	switch  (ucEventID)
	{
		case 0xEE:
			MV_RX_BootOn(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xEF:
			MV_RX_OnOff(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x80:
			MV_RX_Start(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x81:
			MV_RX_PairingStatus(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x82:
			MV_RX_PairStatus(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x83:
			MV_RX_ReadPairedDevice(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x84:
			MV_RX_ClearALLPairedDeviceStatus(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x85:
			MV_RX_ClearPairedDeviceStatus(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x86:
			MV_RX_GetDeviceStatus(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x87:
			MV_RX_ReadLocalName(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x88:
			MV_RX_ReadLocalBDAddress(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x89:
			MV_RX_ReadLocalClassOfDevice(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x8A:
			MV_RX_ReadRemoteName(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x8B:
			MV_RX_GetRSSI(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x8D:
			MV_RX_GetPinCode(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x90:
			MV_RX_HF_StateInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x91:
			MV_RX_HF_ConnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x92:
			MV_RX_HF_ConnectProfileInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x93:
			MV_RX_HF_DisconnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x94:
			MV_RX_HF_AudioConnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x95:
			MV_RX_HF_AudioDisconnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x96:
			MV_RX_HF_SpeakerVolumeChangeInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x97:
			MV_RX_HF_ReadSpeakerVolumeRes(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9A:
			MV_RX_HF_MicMuteStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9B:
			MV_RX_HF_AnswerCallStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9C:
			MV_RX_HF_HangUpCallStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9D:
			MV_RX_HF_RejectCallStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9E:
			MV_RX_HF_LastNumberRedialStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x9F:
			MV_RX_HF_NumberDialStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA0:
			MV_RX_HF_DTMFToneStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA1:
			MV_RX_HF_VoiceDialActivationStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;			
		case 0xA3:
			MV_RX_HF_CallsReleaseHeldOrRejectWaitingStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA4:
			MV_RX_HF_CallsReleaseActiveAcceptOtherStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA5:
			MV_RX_HF_CallsHoldActiveAcceptOtherStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA6:
			MV_RX_HF_CallAddHeldCallStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA7:
			MV_RX_HF_CallsExplicitCallTransferStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA8:
			MV_RX_HF_GetNetworkOperatorInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xA9:
			MV_RX_HF_GetSubscriberNumberInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAA:
			MV_RX_HF_GetCurrentCallInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAB:
			MV_RX_HS_SendButtonPressCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAC:
			MV_RX_HF_RingInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAD:
			MV_RX_HF_InbandRingInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAE:
			MV_RX_HF_CallInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xAF:
			MV_RX_HF_CallerID(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xB1:
			MV_RX_HF_AudioTransferToAGInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xB2:
			MV_RX_HF_AudioTransferToHF(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xB3:
			MV_RX_HF_SpeedCall(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xB4:
			MV_RX_HF_CallWaitingInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xBE:
			MV_RX_PB_NumOfPhonebook(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xBF:
			MV_RX_PB_PhoneBookDownloadCompleteInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xBC:
			MV_RX_PB_ConnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC7:
			MV_RX_PB_StatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC0:
			MV_RX_AV_StatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC1:
			MV_RX_AV_ConnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC2:
			MV_RX_AV_DisconnectStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC3:
			MV_RX_AV_SpeakerVolumeChangeInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC4:
			MV_RX_AV_ReadSpeakerVolumeRes(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xC5:
			MV_RX_AV_SendRemoteControlCommandStatusCfm(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xCF:
			MV_RX_AVRCP_StatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xD0:
			MV_RX_SYS_FW_VersionRes(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xD2:
			MV_RX_SYS_EnterDUTModeStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xD3:
			MV_RX_SYS_SPK_MIC_LoopbackStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xD4:
			MV_RX_SYS_Enter_DFU_ModeStatusInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xE0:
			MV_RX_UART_REQ_SuccessInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xE1:
			MV_RX_UART_REQ_FailInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0xE2:
			MV_RX_ErrorInd(ucLen, &ucMV_DataBuf[0]);
			break;
		case 0x98:
			MV_RX_HF_MIC_VolumeChangeInd();
			break;
		case 0x99:
			MV_RX_HF_Read_MIC_VolumeRes(&ucMV_DataBuf[0]);
			break;
		default:
			LOG_BT("MV_RX:%X",ucEventID);
			break;
	}
}

void MV_RX_Int(void)
{
	MV_RX_WriteBuf(u0rbl);
	ri_u0c1 = 0;
//	LOG_BT("MV_RX_Int");
}
#ifdef  PBAP_TEST_BUFF
unsigned char ucBBAP_TestBuf[1024 * 15];
unsigned short ucBBAP_TestBufCnt;
#endif

void MV_PB_SkipInit(void)
{
	ucMV_PB_SkipBuf[0] = 0x00;
	ucMV_PB_SkipBuf[1] = 0x00;
	ucMV_PB_SkipBufCnt = 0;
	ucMV_PB_DataSkip = 0;
}

void MV_UartRX_Main(void)
{
	unsigned char ucHeader[3];
	
	if  (usMV_UART_RX_rPtr == usMV_UART_RX_wPtr)
	{
		return;
	}
	switch  (ucMV_RX_Mode)
	{
		case MV_RX_NONE:
			ucHeader[0] = MV_RX_GetData();
			
			if  (ucHeader[0] == 0xCD)
			{
				ucMV_RX_Mode = MV_RX_LENGTH;
			}
			if  (ucHeader[0] == 0x50)
			{
				ucMV_RX_Mode = MV_RX_PB_HEADER;
			}
			break;
		case MV_RX_LENGTH:
			ucMV_RX_Length = MV_RX_GetData();
			ucMV_RX_Mode = MV_RX_DATA;
		case MV_RX_DATA:
			if  (ucMV_RX_Length > MV_RX_GetDataCnt())
			{
			}
			else
			{
				ucMV_RX_Mode = MV_RX_NONE;
				MV_RX_EventMain();
			}
			break;
		case MV_RX_PB_HEADER:
			if  (MV_RX_GetDataCnt() > 1)
			{
				ucHeader[1] = MV_RX_GetData();
				ucHeader[2] = MV_RX_GetData();
				if  ((ucHeader[1] == 0x42) && (ucHeader[2] = 0x3E))
				{
					ucMV_RX_Mode = MV_RX_PB_LENGTH;
				}
				else
				{
					ucMV_RX_Mode = MV_RX_NONE;
				}
			}
			break;
		case MV_RX_PB_LENGTH:
			if  (MV_RX_GetDataCnt() > 1)
			{
				usMV_RX_PB_Length = MV_RX_GetData();
				usMV_RX_PB_Length = (usMV_RX_PB_Length << 8) & 0xFF00;
				usMV_RX_PB_Length = usMV_RX_PB_Length + MV_RX_GetData();
				usMV_RX_PB_Length = usMV_RX_PB_Length - 2;
				ucMV_RX_Mode = MV_RX_PB_EVENT_ID;
				LOG_BT("PB Len:%d",usMV_RX_PB_Length);
			}
			break;
		case MV_RX_PB_EVENT_ID:
			if  (MV_RX_GetDataCnt() > 1)
			{
				unsigned char ucEventID;

				ucEventID = MV_RX_GetData();
				ucMV_RX_PB_MoreData = MV_RX_GetData();
				if  (ucEventID == 0xBB)
				{
					ucMV_RX_Mode = MV_RX_PB_DATA_RECEIVE;// OK
				}
				else
				{
					ucMV_RX_Mode = MV_RX_NONE;
				}
			}
			break;
		case MV_RX_PB_DATA_RECEIVE:
			if  (MV_RX_GetDataCnt() >= usMV_RX_PB_Length)
			{
			#ifdef  PHONE_BOOK
				{
					unsigned char ucRet;
					unsigned char ucData[2];
					
					while(1)
					{
						if  (usMV_RX_PB_Length == 0)
						{
							ucMV_RX_Mode = MV_RX_NONE;
							if	(ucMV_RX_PB_MoreData == 0x01)
							{
								MV_TX_PhoneBookGetNext();
							}
							break;
						}

						if  (ucMV_PB_DataSkip == 0)
						{
							ucRet = PBAP_MakeBlockData(MV_RX_GetData());
							--usMV_RX_PB_Length;

							if  ((ucRet == 1) || (ucRet == 2))
							{
								if  (PBAP_Parsing() == 0)
								{
									ucMV_RX_Mode = MV_RX_NONE;
									MV_TX_PhoneBookAbort();
									break;
								}
								PBAP_InitBlockData();
							}

							if  (ucRet == 2)
							{
								MV_PB_SkipInit();
								ucMV_PB_DataSkip = 1;
							}
						}
						else
						{
							ucMV_PB_SkipBuf[ucMV_PB_SkipBufCnt] = MV_RX_GetData();
							--usMV_RX_PB_Length;
							++ucMV_PB_SkipBufCnt;
							if  (ucMV_PB_SkipBufCnt > 1)
							{
								if  ((ucMV_PB_SkipBuf[0] == 0x0D) && (ucMV_PB_SkipBuf[1] == 0x0A))
								{
									MV_PB_SkipInit();
								}
								else
								{
									--ucMV_PB_SkipBufCnt;
									ucMV_PB_SkipBuf[0] = ucMV_PB_SkipBuf[1];
									ucMV_PB_SkipBuf[1] = 0x00;
								}
							}
						}
					}
				}
			#endif
			}
			break;
	}
}

#endif
